/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ControlEscolar.AdminExam;

import com.ControlEscolar.Beans.ExamRequest;
import com.ControlEscolar.Services.ExamRequestService;
import java.sql.Date;
import java.sql.Time;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

/**
 *
 * @author Jalbs
 */
public class AdminExamRequest extends MappingDispatchAction{

    private ExamRequestService service=new ExamRequestService();
    private static final String MODIFY="m";

    public ActionForward addExamRequest (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{

          ExamRequestForm examRequestForm = (ExamRequestForm)form;
          ExamRequest examRequest=formToBean(examRequestForm);
          System.out.println(examRequest.toString());
          try{            
            service.createExamRequest(examRequest);
            examRequestForm.setAction("s");
            return mapping.findForward("success");
          }catch(Exception ex){
            ex.printStackTrace();
            examRequestForm.setAction("f");
            return mapping.findForward("failed");
          }

    }

    public ActionForward getAllExamRequests (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{
          List examRequests;
          ExamRequestForm examRequestForm = (ExamRequestForm)form;
          examRequests=service.getAllExamRequests();
          HttpSession session = request.getSession();
          session.setAttribute("examRequests", examRequests);
          return mapping.findForward("success");          

    }
    
    public ActionForward getExamRequestToModify (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{
          String examKey=request.getParameter("id");
          ExamRequest examRequest = new ExamRequest();
          examRequest = service.getExamRequestById(Long.parseLong(examKey.trim()));
          ExamRequestForm examRequestForm = (ExamRequestForm)form;
          beanToForm(examRequestForm, examRequest);
          examRequestForm.setAction(MODIFY);
          return mapping.findForward("success");

    }

    public void modifyState (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{

          ExamRequestForm examRequestForm = (ExamRequestForm)form;
          ExamRequest examRequest=formToBean(examRequestForm);
          examRequestForm.setAction("");
          System.out.println(examRequest.toString());
          try{
          service.updateExamRequest(examRequest);
          response.getWriter().print("success");
          }catch(Exception ex){
            ex.printStackTrace();
            response.getWriter().print("No se puede modificar esta solicitud");
          }

    }

    private ExamRequest formToBean(ExamRequestForm examRequestForm) {
        ExamRequest examRequest = new ExamRequest();
        if(examRequestForm.getID_Request()!=null)
        examRequest.setID_Request(Long.parseLong(examRequestForm.getID_Request().trim()));
        examRequest.setApplicationDate(stringToDate(examRequestForm.getApplicationDate()));
        examRequest.setApplicationTime(stringToTime(examRequestForm.getApplicationTime()));
        examRequest.setStatus(examRequestForm.getStatus());
        examRequest.setDescription(examRequestForm.getDescription());

        return examRequest;
    }

    private void beanToForm(ExamRequestForm examRequesetForm, ExamRequest examRequest) {
        examRequesetForm.setApplicationDate(dateToString(examRequest.getApplicationDate()));
        examRequesetForm.setApplicationTime(timeToString(examRequest.getApplicationTime()));
        examRequesetForm.setStatus(examRequest.getStatus());
        examRequesetForm.setID_Request(Long.toString(examRequest.getID_Request()));
        examRequesetForm.setDescription(examRequest.getDescription());
    }

    private Date stringToDate(String yearMonthDay){
        if(yearMonthDay!=null){
            String[] splittedDate=yearMonthDay.split("/");
            if(splittedDate.length==3){
                Date date=Date.valueOf(""+splittedDate[0]+"-"+splittedDate[1]+"-"+splittedDate[2]);
                return date;
            }
            splittedDate=yearMonthDay.split("-");
            if(splittedDate.length==3){
                Date date=Date.valueOf(""+splittedDate[0]+"-"+splittedDate[1]+"-"+splittedDate[2]);
                return date;
            }
        }
        return null;
    }

    private String dateToString(Date date){
        String[] ymd=date.toString().split("-");
        String s=""+(ymd[0]+"-"+ymd[1]+"-"+ymd[2]);
        return s;
    }

    private Time stringToTime(String time){
        if(time.trim().compareTo("")!=0){
            Time sqlTime=Time.valueOf(time);
            return sqlTime;
        }
        return null;
    }

    private String timeToString(Time time){
        return time.toString();
    }

}
